﻿using System.Linq;
using System.Web.Mvc;
using DLMemberPointMS.Business;
using DLMemberPointMS.Entity.POCOModel;
using DLMemberPointMS.Entity.ViewModel;
using DLMemberPointMS.Common;
using System.Data;
using System;
using DLMemberPointMS.Web.Configs;
using System.Web;
using System.Drawing;
using System.IO;

namespace DLMemberPointMS.Web.Controllers
{
    [Authorize]
    //[WebApiExceptionFilterAtrribute]
    public class MemberController : Controller
    {
        readonly MemCardsService _memCardsService = new MemCardsService();
        /// <summary>
        /// 会员信息视图
        /// </summary>
        /// <returns></returns>
        // [ActionFilter]

        // [Authorize]
        public ActionResult MemberList()
        {
            var level = EnumHelper.EnumListDic<CardLevelsEnum>("请选择", "");
            var levelList = new SelectList(level, "value", "key");
            ViewData["levels"] = levelList;
            var state = EnumHelper.EnumListDic<StateEnum>("请选择", "");
            var selectListItems = new SelectList(state, "value", "key");
            ViewData["state"] = selectListItems;
            return View();
        }
        /// <summary>
        /// 返回会员列表
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
      //  [HttpPost]
        public ActionResult MemcardListResult(MemCardsModel mod)
        {
            return Json(_memCardsService.GetData(mod), JsonRequestBehavior.AllowGet);

        }
        /// <summary>
        /// 修改会员信息视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EditMember(string id)
        {
            var memView = new MemCards();
            using (var db = new DLMPMSModel())
            {
                memView = db.MemCards.Where(m => m.MC_CardID == id).SingleOrDefault();
            }
            //会员等级
            var level = EnumHelper.EnumListDic<CardLevelsEnum>("请选择", "0");
            var levelList = new SelectList(level, "value", "key");
            ViewData["levels"] = levelList;

            //卡状态
            var state = EnumHelper.EnumListDic<StateEnum>("请选择", "0");
            var selectListItems = new SelectList(state, "value", "key");
            ViewData["state"] = selectListItems;

            //性别
            var Sex = EnumHelper.EnumListDic<MemberSexEnum>("未知", "-1");
            var selectSexList = new SelectList(Sex, "value", "key");
            ViewData["sex"] = selectSexList;

            return View(memView);
        }
        /// <summary>
        /// 修改卡状态视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult ChangeState(string id)
        {
            var viewModel = _memCardsService.ChangeState(id);
            //卡状态
            var state = EnumHelper.EnumListDic<StateEnum>("请选择", "0");
            var selectListItems = new SelectList(state, "value", "key");
            ViewData["state"] = selectListItems;
            return View(viewModel);
        }
        /// <summary>
        /// 修改卡状态
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ChangeState(MemCardSEditViewModel mod)
        {
            var res = _memCardsService.ChangeStateResult(mod);
            return Json(res);
        }
        /// <summary>
        /// 修改会员详细信息
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditMemberResult(MemCardSEditViewModel mod)
        {
            var res = _memCardsService.EditMemCard(mod);
            return Json(res);
        }
        /// <summary>
        /// 添加会员
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddMemberResult(MemCardSEditViewModel mod)
        {
            var res = _memCardsService.AddMemCard(mod);
            return Json(res);
        }
        /// <summary>
        /// 删除会员
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DeleteMemberResult(string id)
        {
            var res = _memCardsService.DeleteMember(id);
            return Json(res);
        }
        /// <summary>
        /// 显示转出会员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult MemTransfer(string id)
        {
            var res = _memCardsService.TransferOut(id);
            return View(res);
        }
        /// <summary>
        /// 显示转入会员信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult MemTransferResult(string id)
        {
            var res = _memCardsService.TransferIn(id);
            return Json(res);

        }
        /// <summary>
        /// 执行会员转账
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult StartTransfer(MemCardSEditViewModel mod)
        {
            mod.UserID = 1;
            var res = _memCardsService.StartTransfer(mod);
            return Json(res);
        }
        /// <summary>
        /// 会员换卡
        /// </summary>
        /// <param name="mod"></param>
        /// <returns></returns>
        public ActionResult ChangeCard(string id)
        {
            var res = _memCardsService.ChangeCardView(id);
            return View(res.data);
        }
        public ActionResult ChangeCardResult(MemCardSEditViewModel viewmod)
        {
            var res = _memCardsService.ChangeCardRes(viewmod);
            return Json(res);
        }
        public ActionResult GetUploadUrl(HttpPostedFileBase file)
        {
            var src = $"/UploadFile/{Guid.NewGuid() + file.FileName}";
            file.SaveAs(Server.MapPath(src));      //保存文件
            var res = new OperateResult(code.Success, "ok", src);
            return Json(res);
        }
        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ExportResult()
        {
            OutToExcel.OutExcel();


            var res = new OperateResult(code.Success, "导出成功，文件位于D盘根目录哦！");
            return Json(res);
        }

        //public JsonResult Upload(HttpPostedFileBase uploadedFile)
        //{
        //    if (uploadedFile != null && uploadedFile.ContentLength > 0)
        //    {
        //        byte[] FileByteArray = new byte[uploadedFile.ContentLength];
        //        uploadedFile.InputStream.Read(FileByteArray, 0, uploadedFile.ContentLength);
        //        Attachment newAttchment = new Attachment();
        //        newAttchment.FileName = uploadedFile.FileName;
        //        newAttchment.FileType = uploadedFile.ContentType;
        //        newAttchment.FileContent = FileByteArray;
        //        OperationResult operationResult = attachmentManager.SaveAttachment(newAttchment);
        //        if (operationResult.Success)
        //        {
        //            string HTMLString = CaptureHelper.RenderViewToString
        //          ("_AttachmentItem", newAttchment, this.ControllerContext);
        //            return Json(new
        //            {
        //                statusCode = 200,
        //                status = operationResult.Message,
        //                NewRow = HTMLString
        //            }, JsonRequestBehavior.AllowGet);

        //        }
        //        else
        //        {
        //            return Json(new
        //            {
        //                statusCode = 400,
        //                status = operationResult.Message,
        //                file = uploadedFile.FileName
        //            }, JsonRequestBehavior.AllowGet);

        //        }
        //    }
        //    return Json(new
        //    {
        //        statusCode = 400,
        //        status = "Bad Request! Upload Failed",
        //        file = string.Empty
        //    }, JsonRequestBehavior.AllowGet);
        //}

        public ActionResult UploadImg()
        {
            return View();
        }
        //上传图片
        public ActionResult UploadImgRes(HttpPostedFileBase file)
        {
            //获取存储的路径
            string path = Server.MapPath(string.Format("~/{0}/", "UploadImg"));
            //截取图片后缀名
            string fileName = file.FileName.Substring(file.FileName.LastIndexOf("."));
            //转换时间
            string time = DateTime.Now.ToString("yyyy-MM-dd");
            //拼接文件名，时间+4位随机数+原文件名
            fileName = time + "-" + new Random().Next(1001, 9999) + fileName;
            //保存文件
            string Name = Path.Combine(path, fileName);
            file.SaveAs(Name);
            LogHelper.Info(Name + "上传成功");
            //得到文件的保存地址 并将文件保存地址放入到数据库中(没写)
            //var res = _memCardsService.UploadImgSaveDb(path);
            return Json(new OperateResult(code.Success, "上传成功"));
        }
    }
}
